package com.ztc.ZtcTransportPercentageScale.service;

import com.aqgj.util.JsonWrapper;
import com.ztc.ZtcTransport.bean.ZtcTransport;
import com.ztc.ZtcTransport.dao.ZtcTransportDao;
import com.ztc.ZtcTransportPercentageScale.bean.ZtcTransportPercentageScale;
import com.ztc.ZtcTransportPercentageScale.bean.ZtcTransportPercentageVo;
import com.ztc.ZtcTransportPercentageScale.dao.ZtcTransportPercentageScaleDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author 2017/11/19
 */
@Service("ZtcTransportPercentageScale")
public class ZtcTransportPercentageScaleManager{

    @Autowired
    ZtcTransportPercentageScaleDao ztcTransportPercentageScaleDao;

    @Autowired
    ZtcTransportDao ztcTransportDao;

    public ZtcTransportPercentageScale findUniqueBy(String propertyName, Object value) {
        return ztcTransportPercentageScaleDao.findUniqueBy(propertyName, value);
    }

    public List<ZtcTransportPercentageScale> findBy(String propertyName, Object value) {
        return ztcTransportPercentageScaleDao.findBy(propertyName, value);
    }

    public String save(ZtcTransportPercentageScale ztcTransportPercentageScale) {
        ztcTransportPercentageScaleDao.save(ztcTransportPercentageScale);
        return ztcTransportPercentageScale.getId();
    }

    public ZtcTransportPercentageScale get(String zid) {
        return ztcTransportPercentageScaleDao.get(zid);
    }

    public String update(ZtcTransportPercentageScale ztcTransportPercentageScale) {
        return ztcTransportPercentageScaleDao.update(ztcTransportPercentageScale);
    }

    /**
     * 获取当前公司旗下公司提成比例信息
     * @param ztcTransport 当前session公司
     */
    public HashMap<String,Object> getPercentageScaleInfo(ZtcTransport ztcTransport){
     ZtcTransportPercentageScale ztcTransportPercentageScale=ztcTransportPercentageScaleDao.findUniqueBy("transportId",ztcTransport.getId());
        ZtcTransportPercentageVo ztcTransportPercentageVo=new ZtcTransportPercentageVo();
        ztcTransportPercentageVo.setTransportId(ztcTransport.getId());
        ztcTransportPercentageVo.setTransportFullName(ztcTransport.getFullName());
        ztcTransportPercentageVo.setTransportType(ztcTransport.getType());
        //运营公司需要查询一级分销提成
        if(ztcTransport.getType()==3){
            ztcTransportPercentageVo.setDealerUserLevelScale(ztcTransport.getDistributorRebateProportion());
        }
        if(ztcTransportPercentageScale!=null) {
            ztcTransportPercentageVo.setTransportIncomeScale(ztcTransportPercentageScale.getIncomeScale().toString());
            ztcTransportPercentageVo.setModifyUsername(ztcTransportPercentageScale.getModifyUsername());
            ztcTransportPercentageVo.setModifyTransportName(ztcTransportPercentageScale.getModifyTransportName());
            ztcTransportPercentageVo.setModifyTime(ztcTransportPercentageScale.getModifyTime() == null ? null : ztcTransportPercentageScale.getModifyTime().toString());
            ztcTransportPercentageVo.setModifyRemark(ztcTransportPercentageScale.getModifyRemark());
            ztcTransportPercentageVo.setCreateTime(ztcTransportPercentageScale.getCreateTime() == null ? null : ztcTransportPercentageScale.getCreateTime().toString());
        }
        List<ZtcTransportPercentageVo> ztcTransportPercentageVos1= gets(ztcTransport.getId());
        ztcTransportPercentageVo.setChildZtcTransportPercentageVos(ztcTransportPercentageVos1);
      return JsonWrapper.successWrapper(ztcTransportPercentageVo);
    }

    /**
     * 递归循环
     * @param ztcTransportId 公司id
     * @return
     */
    private List<ZtcTransportPercentageVo> gets(String ztcTransportId) {
        List<ZtcTransportPercentageVo> result = getZtcTransportPercentageVos(ztcTransportId);
        List<ZtcTransportPercentageVo> result1 = new ArrayList<>();
        for (ZtcTransportPercentageVo z : result) {
            z.setChildZtcTransportPercentageVos(gets(z.getTransportId()));
            result1.add(z);
        }
        return result1;
    }

    /**
     * 封装结果返回对象
     * @param ztcTransportId 公司id
     * @return
     */
    private List<ZtcTransportPercentageVo> getZtcTransportPercentageVos(String ztcTransportId) {
        List<Object[]> list=ztcTransportPercentageScaleDao.getPercentageScaleInfo(ztcTransportId);
        List<ZtcTransportPercentageVo> ztcTransportPercentageVos=new ArrayList<>();
        if(list.size()>0){
            for (Object[] o:list) {
                ZtcTransportPercentageVo ztcTransportPercentageVo=new ZtcTransportPercentageVo(o[0].toString(),o[1].toString(),Integer.valueOf(o[2].toString()),o[3].toString());
                if(Integer.valueOf(o[2].toString())==3){
                    ztcTransportPercentageVo.setDealerUserLevelScale(ztcTransportDao.get(o[0].toString()).getDistributorRebateProportion());
                }
                ztcTransportPercentageVo.setCreateTime(o[4].toString());
                ztcTransportPercentageVo.setModifyUsername(o[6].toString());
                ztcTransportPercentageVo.setModifyTime(o[7].toString());
                ztcTransportPercentageVo.setModifyRemark(o[8].toString());
                ztcTransportPercentageVo.setModifyTransportName(o[5].toString());
                ztcTransportPercentageVos.add(ztcTransportPercentageVo);
            }
        }
        return ztcTransportPercentageVos;
    }

}
